package com.net;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.greenentity.Defect;
import com.greenentity.Inspect;
import com.greenentity.Logger;
import com.greenentity.Photo;
import com.greenentity.Ready;
import com.greenentity.SafetyCard;
import com.greenentity.SafetyTool;
import com.greenentity.Task;
import com.greenentity.TourDefect;
import com.interfaces.NovaCallback;
import com.net.callback.Callback;
import com.substation.utils.ZipUtils;
import com.tamic.novate.Novate;
import com.tamic.novate.Throwable;
import com.tamic.novate.callback.RxStringCallback;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by admin on 2018/5/21.
 */

public class SubstationNet extends BaseNet {

    public static void getSubtation(Context context, NovaCallback c) {
        Map<String, Object> map = getParams(context);
        getNovateOfDialog(context, "数据加载中...").rxGet(Contants.getSubstation, map, new Callback(2, c, d));
    }

    public static void getTourSubsWork(Context context, NovaCallback c) {
        Map<String, Object> map = getParams(context);
        map.put("types", "20");
        getNovateOfDialog(context, "数据加载中...").rxPost(Contants.getTourSubsWork, map, new Callback(1, c, d));
    }

    public static void downGsubslog(Context context, NovaCallback c) {
        getNovateOfDialog(context, "数据加载中...").rxGet(Contants.downGsubslog, getParams(context), new Callback(1, c, d));
    }

    public static void uploadGsubslog(Context context, Logger logger, NovaCallback c) {
        Map<String, Object> map = getParams(context);
        map.put("gsubslogJson", JSON.toJSONString(logger));
        getNovateOfDialog(context, "数据上传中...").rxPost(Contants.uploadGsubslog, map, new Callback(2, c, d));
    }

    public static void downTourSubsWorkByGuid(Context context, String guid, NovaCallback c) {
        Map<String, Object> map = getParams(context);
        map.put("guid", guid);
        getNovateOfDialog(context, "正在下载...").rxPost(Contants.downTourSubsWorkByGuid, map, new Callback(2, c, d));
    }

    public static void upload(Context context, Task task, List<Inspect> inspects,
                              List<Defect> defects, List<SafetyCard> safetyCards, List<Ready> readies, List<SafetyTool> safetyTools, List<Photo> photos, NovaCallback c) {
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data; charset=utf-8"), new File(ZipUtils.zipFilePath));
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("attachment", "zip", requestFile)
                .addFormDataPart("editJson", JSON.toJSONString(photos))
                .addFormDataPart("task", JSON.toJSONString(task))
                .addFormDataPart("inspects", JSON.toJSONString(inspects))
                .addFormDataPart("defects", JSON.toJSONString(defects))
                .addFormDataPart("safetyCards", JSON.toJSONString(safetyCards))
                .addFormDataPart("readies", JSON.toJSONString(readies))
                .addFormDataPart("safetyTools", JSON.toJSONString(safetyTools))
                .build();
        getNovateOfDialog(context, "数据上传中...").rxUploadWithBody("", Contants.upload, requestBody, new Callback(1, c, d));
    }

    public static void getTourDefect(Context context, NovaCallback c) {
        getNovateOfDialog(context, "Loading...").rxPost(Contants.getTourDefect, getParams(context), new Callback(1, c, d));
    }

    public static void uploadTourDefect(Context context, TourDefect tourDefect, List<Photo> photos, NovaCallback c) {
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data; charset=utf-8"), new File(ZipUtils.zipFilePath));
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("attachment", "zip", requestFile)
                .addFormDataPart("photoJson", JSON.toJSONString(photos))
                .addFormDataPart("defectJson", JSON.toJSONString(tourDefect))
                .build();
        getNovateOfDialog(context, "图片上传中...").rxUploadWithBody("", Contants.uploadTourDefect, requestBody, new Callback(2, c, d));
    }

    public static void getWeather(Context context, final NovaCallback c) {
        getDialog(context, "正在获取天气....");
        new Novate.Builder(context).baseUrl("https://free-api.heweather.com/s6/weather/").addHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        }).connectTimeout(30).readTimeout(30).addSSLSocketFactory(createSSLSocketFactory()).addLog(true).addHeader(getHead(context)).build()
                .rxGet("now?location=CN101110805&key=bd93f0bfafab44c083f2a390287852b4", getParams(context), new RxStringCallback() {
                    @Override
                    public void onNext(Object tag, String response) {
                        Log.d("nova", response);
                        d.dismiss();
                        d.cancel();
                        c.success(3, response);
                    }

                    @Override
                    public void onError(Object tag, Throwable e) {
                        d.dismiss();
                        d.cancel();
                        c.err(3, -3, e.getMessage());
                    }

                    @Override
                    public void onCancel(Object tag, Throwable e) {
                        d.dismiss();
                        d.cancel();
                    }
                });
    }

}
